#include "GuiPlugin2ConfigPanel.h"

#include <QFormLayout>

#include <plugin/util/Framework.h>
using namespace antu::plugin;

GuiPlugin2ConfigPanel::GuiPlugin2ConfigPanel()
{
    a = new int[3];

    for (int i = 0; i < 3; i++)
        a[i] = 0;

    QFormLayout* p = new QFormLayout;
    p->addRow("1", (p1 = new QSpinBox));
    p->addRow("2", (p2 = new QSpinBox));
    p->addRow("3", (p3 = new QSpinBox));

    this->setLayout(p);
}

GuiPlugin2ConfigPanel::~GuiPlugin2ConfigPanel()
{
    delete [] a;
}

void GuiPlugin2ConfigPanel::save() const
{
    a[0] = this->p1->value();
    a[1] = this->p2->value();
    a[2] = this->p3->value();

    Framework::Instance.settingsManager()->beginWriteArray("guiplugin2", 3);

    for (int i = 0; i < 3; i++)
    {
        Framework::Instance.settingsManager()->setArrayIndex(i);
        Framework::Instance.settingsManager()->setValue("guiplugin2", a[i]);
    }
    Framework::Instance.settingsManager()->endArray();
    Framework::Instance.settingsManager()->sync();
}

void GuiPlugin2ConfigPanel::load()
{
    int size = Framework::Instance.settingsManager()->beginReadArray("guiplugin2");

    for (int i = 0; i < size; i++)
    {
        Framework::Instance.settingsManager()->setArrayIndex(i);
        a[i] = Framework::Instance.settingsManager()->value("guiplugin2").toInt();
    }
    Framework::Instance.settingsManager()->endArray();

    p1->setValue(a[0]);
    p2->setValue(a[1]);
    p3->setValue(a[2]);
}

quint64 GuiPlugin2ConfigPanel::computeContentHash() const
{
    return a[0] * 10000 + a[1] * 100 + a[2];
}
